function initLogin(callBack) {
    startLogin("",callBack);

    mui('body').on("tap","#wechatLogin",function () {
        var url = location.href;
        url = url.substring(url.indexOf("app/page"),url.length);
        mui.openWindow({
            url:_basePath + "app/page/mine_wxlogin?url=" + url
        })
    });

    mui('body').on("tap","#register",function () {
        console.log("开始注册流程...")
        startRegister("",callBack);
    });

    mui('body').on("tap","#forget",function () {
        var mobile = $("#mobile").val();
        if(!isMobile(mobile)){
            mui.toast("手机号码格式不正确");
        }else{
            validateMobile(mobile,"forget",callBack);
        }
    });

    mui('body').on("tap",".verifyBtn",function () {
        var mobile = $("#mobile").val();
        if(!isMobile(mobile)){
            mui.toast("手机号码格式不正确");
        }else{
            sendVerifyCode(mobile,callBack);
        }
    });

}

//登录第一步输入手机号
function startLogin(mobile,callBack) {
    $.dialog({
        titleText : '使用账号和密码登录',
        type : 'confirm',
        buttonText : {
            ok : '下一步',
            cancel : '取消'
        },
        onClickOk : function(){
            var mobile = $("#mobile").val();
            if(!isMobile(mobile)){
                mui.toast("手机号码格式不正确");
                return false;
            }
            nextStep(mobile,callBack);
        },
        onClickCancel : function(){
            return true;
        },
        contentHtml : '<div class="dialog_input"><input type="text" class="login_input" id="mobile" value="' + mobile + '" placeholder="请输入手机号码" /></div>',
        additionalHtml : '<div id="wechatLogin" class="mask"><div class="wechatLogo"><img src="' + _basePath + '/app/images/wechat.png"></div><span>用微信账号登录</span></div><div id="register" class="mask">注册</div>'
    });
}

//登录第二步输入密码
function nextStep(mobile,callBack) {
    $.dialog({
        titleText : mobile,
        type : 'confirm',
        buttonText : {
            ok : '登录',
            cancel : '上一步'
        },
        onClickOk : function(){
            var pwd = $("#password").val();
            if(isEmpty(pwd)){
                mui.toast("请输入密码");
                return false;
            }
            $.ajax({
                url: _basePath + "app/member/login",
                data: {
                  mobile : mobile,
                  password : pwd
                },
                dataType: 'json',
                success: function (data) {
                    if(typeof data != undefined && data.resCode == 0){
                        mui.toast("登录成功");
                    }else{
                        mui.toast(data['message']);
                    }
                    if(typeof callBack == 'function'){
                        callBack();
                    }
                }
            });
            return true;
        },
        onClickCancel : function(){
            startLogin(mobile);
        },
        contentHtml : '<input type="hidden" id="mobile" value="' + mobile + '"/><div class="dialog_input"><input type="password" class="login_input" id="password" placeholder="请输入密码" /></div>',
        additionalHtml : '<div id="forget" class="mask">忘记密码?</div>'
    });
}

//注册第一步输入手机号
function startRegister(mobile,callBack) {
    $.dialog({
        titleText : '注册账号',
        type : 'confirm',
        buttonText : {
            ok : '下一步',
            cancel : '取消'
        },
        onClickOk : function(){
            var mobile = $("#mobile").val();
            if(!isMobile(mobile)){
                mui.toast("手机号码格式不正确");
                return false;
            }
            checkMobile(mobile,function () {
                validateMobile(mobile,"register",callBack);
            })
        },
        onClickCancel : function(){
            return true;
        },
        contentHtml : '<div class="dialog_input"><input type="text" class="login_input" id="mobile" value="' + mobile + '" placeholder="请输入手机号码" /></div>',
    });
}
//注册第二步手机验证码
function validateMobile(mobile,type,callBack) {
    $.dialog({
        titleText : mobile,
        type : 'confirm',
        buttonText : {
            ok : '下一步',
            cancel : '取消'
        },
        onClickOk : function(){
            var verifyCode = $("#validateCode").val();
            if(isEmpty(verifyCode)){
                mui.toast("请输入验证码");
                return false;
            }
            checkValidateCode(verifyCode,function () {
                setPwd(mobile,type,callBack);
            });
        },
        onClickCancel : function(){
            if(type == "register"){
                startRegister(mobile,callBack);
            }else if(type == "forget"){
                return true;
            }
        },
        contentHtml : '<input type="hidden" id="mobile" value="' + mobile + '"/><div class="dialog_input"><input type="text" class="login_input" style="width: 60%" id="validateCode" placeholder="请输入验证码" /><a class="verifyBtn on">获取验证码</a></div>',
    });
}

function checkMobile(mobile,callback) {
    $.ajax({
        url: _basePath + "app/member/checkMobile",
        data: {
            mobile : mobile
        },
        dataType: 'json',
        success: function (data) {
            if(typeof data != undefined && data.resCode == 0){
                if(typeof callback == 'function'){
                    callback();
                }
            }else{
                mui.toast(data['message']);
            }
        }
    });
}

function checkValidateCode(verifyCode,callBack) {
    $.ajax({
        url: _basePath + "app/member/noteCodeVerify",
        data: {
            verifyCode : verifyCode
        },
        dataType: 'json',
        success: function (data) {
            if(typeof data != undefined && data.resCode == 0){
                if(typeof callBack == 'function'){
                    callBack();
                }
            }else{
                mui.toast(data['message']);
            }
        }
    });
}
function sendVerifyCode(mobile) {
    $.ajax({
        url: _basePath + "app/member/noteCodeSend",
        data: {
            mobile : mobile
        },
        dataType: 'json',
        success: function (data) {
            if(typeof data != undefined && data.resCode == 0){
                mui.toast('发送成功');
                codeTime($(".verifyBtn"))
            }else{
                mui.toast(data['message']);
            }
        }
    });
}
var wait = 60;
function codeTime(obj) {
    if (wait == 0) {
        $(obj).attr('href','javascript:sendVerifyCode()');
        $(obj).text("获取验证码");
        $(obj).addClass("on");
        wait = 60;
    } else {
        $(obj).attr("href","javascript:;");
        $(obj).text("重新发送(" + wait + ")");
        $(obj).removeClass("on");
        wait--;
        setTimeout(function () {
            codeTime(obj)
        },1000)
    }
}
//注册第三步设置密码
function setPwd(mobile,type,callBack) {
    $.dialog({
        titleText : '设置密码',
        type : 'confirm',
        buttonText : {
            ok : '完成',
            cancel : '取消'
        },
        onClickOk : function(){
            var pwd = $("#password").val();
            var re_pwd = $("#re_password").val();
            if(isEmpty(pwd)){
                mui.toast("密码不能为空");
                return false;
            }
            if(pwd != re_pwd){
                mui.toast("两次密码不一致");
                return false;
            }
            if(type == "register"){
                finishRegister(mobile,pwd,callBack);
            }else{
                finishForget(mobile,pwd,callBack);
            }
            return true;
        },
        onClickCancel : function(){
            validateMobile(mobile,callBack);
        },
        contentHtml : '<div class="dialog_input"><input type="password" class="login_input" id="password" placeholder="设置密码" /></div><div><input type="password" class="login_input" id="re_password" placeholder="重复密码" /></div>',
    });
}
function finishRegister(mobile,pwd,callBack) {
    $.ajax({
        url: _basePath + "app/member/register",
        data: {
            mobile:mobile,
            password:pwd
        },
        dataType: 'json',
        success: function (data) {
            if(typeof data != undefined && data.resCode == 0){
                mui.toast("注册成功");
            }else{
                mui.toast(data['message']);
            }
            if(typeof callBack == 'function'){
                callBack();
            }
        }
    });
}

function finishForget(mobile,pwd,callBack) {
    $.ajax({
        url: _basePath + "app/member/pwdEdit",
        data: {
            mobile:mobile,
            pwdNew:pwd
        },
        dataType: 'json',
        success: function (data) {
            if(typeof data != undefined && data.resCode == 0){
                mui.toast("密码修改成功");
            }else{
                mui.toast(data['message']);
            }
            if(typeof callBack == 'function'){
                callBack();
            }
        }
    });
}